package com.jsb.laundry.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.jsb.laundry.common.enums.ReturnStatusCode;
import com.jsb.laundry.common.response.WebResponse;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.jhh.utils.Withdrawal;
import com.jsb.laundry.model.dto.ConductCertificationRequest;
import com.jsb.laundry.model.dto.IdInfoRequest;
import com.jsb.laundry.model.dto.RiderWithdrawalLogRequest;
import com.jsb.laundry.model.vo.RiderWithdrawalLogVo;
import com.jsb.laundry.service.RealNameAuthenticationService;
import com.jsb.laundry.service.RiderUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Api(tags = {"实名认证"})
@RestController
@RequestMapping("/realNameAuthentication/")
public class RealNameAuthenticationController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RealNameAuthenticationController.class);
    @Resource
    private RealNameAuthenticationService realNameAuthenticationService;
    @Resource
    private RiderUserInfoService userInfoService;
    @ApiOperation(value = "查询骑手是否认证", notes = "查询骑手是否认证", httpMethod = "POST")
    @PostMapping("idInfo")
    public WebResponse idInfo(@RequestBody IdInfoRequest idInfoRequest) {
        String byUserId = realNameAuthenticationService.findByUserId(idInfoRequest.getUserId());
        return WebResponse.successData(byUserId);
    }

    /**
     * 待定 后期可能会修改
     * @param conductCertificationRequest
     * @return
     */
    @ApiOperation(value = "进行实名认证", notes = "进行实名认证", httpMethod = "POST")
    @PostMapping("conductCertification")
    public WebResponse conductCertification(@RequestBody ConductCertificationRequest conductCertificationRequest) {
        if(JSBUtil.isEmptyString(conductCertificationRequest.getUserId())){
            return WebResponse.success(ReturnStatusCode.PLEASE_FILL_IN_USERID.getCode(), ReturnStatusCode.PLEASE_FILL_IN_USERID.getMsg());
        }
        //进行实名认证添加入口
        int certification = realNameAuthenticationService.conductCertification(conductCertificationRequest);
        //第三方
        String s = Withdrawal.realName(conductCertificationRequest.getUserIdNumber(), conductCertificationRequest.getRealName(), conductCertificationRequest.getUserPhone(), conductCertificationRequest.getBankCardId(), conductCertificationRequest.getIdNumberFront(), conductCertificationRequest.getIdNumberReverse());

        //实名认证后，修改laundry_rider_info表中实名认证状态real_name_auth_status
        JSONObject jsonObject = JSONObject.parseObject(s);
        Integer status = jsonObject.getInteger("status");
        Integer realStatus = jsonObject.getInteger("real_status");
        String resultMsg = jsonObject.getString("msg");

        //{"msg":"身份证照片不清晰，请重新上传","param":"","status":-19,"real_status":-191}
        String authStatus = "";
        Boolean realNameAuthStatus = false;
        HashMap map = new HashMap();
        if (status == 200 && realStatus == 200) {
            authStatus = "1"; //实名认证成功
            realNameAuthStatus = true;
        } else {
            authStatus = "2"; //实名认证失败
        }
        if ("该员工已实名认证成功".equals(resultMsg)) {
            realStatus = -1;
            authStatus = "1";
            realNameAuthStatus = false;
            resultMsg = "该身份证已认证";
            realStatus = 200;
        }

        map.put("realNameAuthStatus", realNameAuthStatus);
        int var1 = realNameAuthenticationService.updUserInfoRealNameAuthStatus(conductCertificationRequest.getUserId(), authStatus, resultMsg);
        if (var1 == 0) {
            LOGGER.info("RiderEmbodimentController laundryRiderInfoDao.updRiderInfoRealNameAuthStatus(),更新数据异常,入参:{},第三方:{}", conductCertificationRequest, s);
        }
        LOGGER.info("RiderEmbodimentController laundryRiderInfoDao.updRiderInfoRealNameAuthStatus(),12222更新数据异常,入参:{},第三方:{}", conductCertificationRequest, s);
        return WebResponse.successData(String.valueOf(realStatus), resultMsg, map);
//        return WebResponse.successData(certification);
    }

    @ApiOperation(value = "骑手提现列表", notes = "骑手提现列表", httpMethod = "POST")
    @PostMapping("riderWithdrawalLog")
    public WebResponse riderWithdrawalLog(@RequestBody RiderWithdrawalLogRequest riderWithdrawalLogRequest){
        PageHelper.startPage(riderWithdrawalLogRequest.getPageNum(), riderWithdrawalLogRequest.getPageSize());

        List<RiderWithdrawalLogVo> byWithdrawalList = userInfoService.findByWithdrawalList(riderWithdrawalLogRequest.getUserId());
        for (RiderWithdrawalLogVo riderWithdrawalLogVo : byWithdrawalList) {
            if ("0".equals(riderWithdrawalLogVo.getReviewStatus())){
                riderWithdrawalLogVo.setReviewStatus("待审核");
            }
            if ("1".equals(riderWithdrawalLogVo.getReviewStatus())){
                riderWithdrawalLogVo.setReviewStatus("审核成功");
            }
            if ("2".equals(riderWithdrawalLogVo.getReviewStatus())){
                riderWithdrawalLogVo.setReviewStatus("审核失败");
            }
            String commissionAmount = riderWithdrawalLogVo.getCommissionAmount();
            double w = Double.parseDouble(commissionAmount);
            riderWithdrawalLogVo.setCommissionAmount("服务费:"+JSBUtil.turnDoubleStrNew(w));
            riderWithdrawalLogVo.setWithdrawalNo("提现编号"+riderWithdrawalLogVo.getWithdrawalNo());
            String money = riderWithdrawalLogVo.getAmount();

            riderWithdrawalLogVo.setAmount(JSBUtil.turnDoubleStrNew(Double.parseDouble(money)));
        }

        return WebResponse.successData(byWithdrawalList);
    }
}
